package com.gemantic.task.handler;


import org.springframework.util.Assert;

import com.gemantic.task.util.SpringUtils;

/**
 * @author xiedi
 * @date 2022/09/16
 **/
public class DefaultTaskHandlerFactoryImpl implements TaskHandlerFactory{

    @Override
    public ITaskHandler getTaskHandler(String taskType) {
        Assert.hasText(taskType, "taskType不允许为空");
        ITaskHandler handler = getTaskHandlerByBeanName(taskType);
        if (handler == null) {
            String beanName = taskType.substring(0, 1).toLowerCase() + taskType.substring(1);
            handler = getTaskHandlerByBeanName(beanName + "TaskHandler");
        }
        Assert.notNull(handler, String.format("不能找到%s的task handler.", taskType));
        return handler;
    }

    private ITaskHandler getTaskHandlerByBeanName(String beanName) {
        if (SpringUtils.containsBean(beanName)) {
            return (ITaskHandler) SpringUtils.getBean(beanName);
        }
        return null;
    }
}
